package com.basswold.nasaesa.client;
import com.basswold.nasaesa.client.ConstructionModule.ModuleType;

/**
 * Used to build a control wing of a mars base.
 * A control wing contains control, power, airlock, and medical modules.
 * @author Keith
 *
 */	
public class DormWingFactory extends BuildingWingFactory {
	/**
	 * Constructor.
	 * @param moduleList Lists structure controlling access to modules.
	 */
	public DormWingFactory(final ContructionList moduleList) {
		super(moduleList);
	}

	@Override
	public final ConstructionConnectorModule startConstruction(
			final ConstructionModule.CardinalDirection direction, boolean airlockIncluded) {
		
		ConstructionConnectorModule connector = null;
		
		try {
			// Airlock requires placement of both sanitation and medical
			// modules next to it as placing a dormitory next to it would
			// violate the layout rules.
			if (airlockIncluded 
				&& modules.getNumModule(ModuleType.PLAIN) >= 2
			    && modules.hasModuleOfType(ModuleType.AIRLOCK) 
				&& modules.hasModuleOfType(ModuleType.MEDICAL)
				&& modules.hasModuleOfType(ModuleType.SANITATION)) {
				
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				this.setEndConnection(connector, 
						modules.getModuleOfType(ModuleType.AIRLOCK), direction);
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.SANITATION), direction);
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.MEDICAL), direction);
				
				// Purposefully did not require a gym to be present for this
				// construction, as this is specific to the requirement for an
				// airlock. Adding a gym is useful, but not required, thus it
				// tries to add one here, but does not require it above.
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.GYM), direction);
				
			} else if (modules.getNumModule(ModuleType.PLAIN) >= 2
					&& modules.getNumModule(ModuleType.DORMITORY) >= 2
					&& modules.hasModuleOfType(ModuleType.SANITATION)
					&& modules.hasModuleOfType(ModuleType.GYM)) {
				// This setup is for the purpose of placing a gym next to a
				// sanitation module.
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				this.setEndConnection(connector, 
						modules.getModuleOfType(ModuleType.DORMITORY), direction);
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.SANITATION), direction);
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.DORMITORY), direction);
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.GYM), direction);
				
			} else if (modules.hasModuleOfType(ModuleType.PLAIN)
					&& modules.hasModuleOfType(ModuleType.DORMITORY)) {
				// Just create a dorm attached to a connector
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				this.setEndConnection(connector, 
						modules.getModuleOfType(ModuleType.DORMITORY), direction);
			} else {
				// cannot create a dorm wing
			}
		} catch (Exception ex) {
			// This should never happen, since we are making sure that all
			// modules needed for construction are present before beginning
			// construction.
			return null;
		}
		
		return connector;
	}

	/*
	 * DORMITORY appears twice because there should be twice as many dormitory
	 * modules as there are sanitation modules (ratio to gym modules does not 
	 * matter).
	 */
	@Override
	protected final ModuleType[] getAcceptableModules() {
		return new ConstructionModule.ModuleType[]
            {ConstructionModule.ModuleType.DORMITORY,
			 ConstructionModule.ModuleType.DORMITORY,
			 ConstructionModule.ModuleType.SANITATION,
			 ConstructionModule.ModuleType.GYM};
	}

	@Override
	public FactoryType getType() {
		return FactoryType.DormWingFactory;
	}	
}
